package com.netease.nertc.demo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

import com.netease.nertc.demo.R;

public class ForegroundServer extends Service {
    private static final int NOTIFICATION_ID = 1;
    private static final String CHANNEL_ID = "UploadChannel";

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 创建并显示前台服务通知
        Notification notification = createNotification("Uploading...");
        startForeground(NOTIFICATION_ID, notification);


        return START_NOT_STICKY;
    }

    private Notification createNotification(String text) {
        return new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("文件上传")
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .build();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "File Upload Channel",
                    NotificationManager.IMPORTANCE_LOW
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }
    public void stopServer(){
        // 停止前台服务并移除通知
        stopForeground(true);
        // 停止服务
        stopSelf();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}